<template>
  <div>
    <div v-if="!user" class="header">
      <!-- 未登录状态 -->
      <div class="top">
        <div>
          <img src="http://liufusong.top:8080/img/profile/bg.png" alt="">
        </div>
      </div>
      <div class="bom">
        <div class="box">
          <div class="imgbox">
            <img src="http://liufusong.top:8080/img/profile/avatar.png" alt="">
          </div>
          <p>游客</p>
          <van-button class="btn" type="primary" size="small" to="/login">登录</van-button>
        </div>
      </div>
    </div>

    <!-- 登入状态 -->
    <div v-else class="header">
      <div class="top">
        <div>
          <img :src="'http://liufusong.top:8080' + userList.avatar" alt="">
        </div>
      </div>
      <div class="bom">
        <div class="box">
          <div class="imgbox">
            <img :src="'http://liufusong.top:8080' + userList.avatar" alt="">
          </div>
          <p>{{ userList.nickname }}</p>
          <van-button type="primary" size="small" @click="quit">退出</van-button>
        </div>
      </div>
    </div>

    <van-grid :column-num="3">
      <van-grid-item icon="star-o" text="我的收藏" @click="collect" />
      <van-grid-item icon="wap-home-o" text="我的出租" @click="rent" />
      <van-grid-item icon="underway-o" text="看房记录" />
      <van-grid-item icon="credit-pay" text="成为房主" />
      <van-grid-item icon="contact" text="个人资料" />
      <van-grid-item icon="service-o" text="联系我们" />
    </van-grid>
    <div class="footer">
      <img src="http://liufusong.top:8080/img/profile/join.png" alt="">
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState } from 'vuex'
import { Dialog } from 'vant'
import { getUser } from '@/api/user.js'
export default {
  data() {
    return {
      userList: []
    }
  },
  computed: {
    ...mapState(['user'])
  },

  created() {
    this.getUser()
  },

  methods: {
    ...mapMutations(['removeUser']),
    quit() {
      Dialog.confirm({
        title: '提示',
        message: '确认退出该账号'
      })
        .then(() => {
          // console.log(123)
          this.removeUser()
        })
        .catch(() => {
          // on cancel
        })
    },
    async getUser() {
      try {
        const res = await getUser()
        // console.log(res)
        this.userList = res.data.body
      } catch (err) {
        console.log(err)
      }
    },
    collect() {
      if (this.user) {
        this.$router.push('/collect')
      } else {
        this.$router.push('/login')
      }
    },
    rent() {
      if (this.user) {
        this.$router.push('/rent')
      } else {
        this.$router.push('/login')
      }
    }
  }
}
</script>

<style scoped lang='less'>
/deep/[class*=van-hairline]::after{
 border: 0;
}
.header{
  position: relative;
  width: 375.2px;
  height: 300px;
}
.top{
height: 191.1px;
width: 375.2px;
img{
width: 100%;
height: 100%;
}
}
.bom{
  height: 165px;
  width: 319px;
}
.box{
  position: absolute;
  top: 125px;
  left: 40px;
  height: 150px;
  width: 300px;
  background-color: #fff;
  text-align: center;
  box-shadow: 0 0 10px 3px #ddd;
}
.imgbox{
  width: 60px;
  height: 60px;
  border-radius: 50%;
  margin-left: 38%;
  margin-top: -35px;
  border: 5px solid #f5f5f5;
  box-shadow: 0 2px 2px #bdbdbd;
  background-color: pink;
  img{
    width: 60px;
    height: 60px;
  }
}
p{
  font-size: 12px;
}

.footer{
  width: 345px;
  height: 85px;
  margin-top: 25px;
  margin-left: 15px;
  img{
    width: 100%;
  }
}
</style>
